<?php
namespace app\admin\model;

use think\Model;
use think\Db;

/**
 * 携号转网错误码
 * @package app\admin\model
 */
class ChangePhoneCode extends Model
{
    protected $table = 'change_phone_code';

    public function getAllData($request)
    {
        $data = $this::field('*');

        if (!empty($request['code'])) {

            $data = $data->where('cp.err_code', 'like', '%'. $request['code'] . '%');
        }

        if (!empty($request['plat_id'])) {

            $data = $data->where('cp.plat_id', '=', $request['plat_id']);
        }

        $data = $data->alias('cp')
            ->field('cp.*, pf.platfrom')
            ->order('cp.id asc')
            ->join("bus_platfrom pf", "cp.plat_id = pf.plat_id", "left")
            ->paginate($request['limit'], false, ['query' => $request])
            ->each(function($item, $key) {

                $item->plat_ = $item->plat_id . $item->platfrom;

                switch ($item->status) {

                    case 0:
                    {
                        $item->status_ = '<span class="layui-badge layui-bg-orange">已禁用</span>';
                        break;
                    }

                    case 1:
                    {
                        $item->status_ = '<span class="layui-badge layui-bg-green">已开启</span>';
                        break;
                    }

                    default:
                    {
                        $item->status_ = '';
                    }
                }

                $item->nickname = 'think';
            });

        return $data;
    }

    public function addOne($data)
    {
        $model = new ChangePhoneCode;

        $data['user']        = session('username');
        $data['create_time'] = date('Y-m-d H:i:s');
        return $model->save($data);
    }

    public function updateOne($data)
    {
        $model = new ChangePhoneCode;

        return $model->save($data, ['id' => $data['id']]);
    }

    public function delOne($data)
    {
        $model = ChangePhoneCode::get($data['id']);
        return $model->delete();
    }

}